package com.xinchao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchao.constant.JSONConstant;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ControllerException;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.DeviceAdvertisement;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertisementMonitorService;
import com.xinchao.service.AdvertisementService;
import com.xinchao.service.DeviceAdvertisementService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 广告Controller（mips平台）
 *
 * @author dxy
 * @date 2018/9/19 9:52
 */
@Controller
@RequestMapping(value = "/api/advertisement")
public class AdvertisementController {
	private static Logger logger = LoggerFactory.getLogger(AdvertisementController.class);
	@Autowired
	private AdvertisementService advertisementService;
	@Autowired
	private AdvertisementMonitorService advertisementMonitorService;
	@Autowired
	private DeviceAdvertisementService deviceAdvertisementService;

	/**
	 * 查询品宣广告
	 */
	@ResponseBody
	@RequestMapping("/queryDeclareGoodsAdvertisement")
	public Object queryDeclareGoodsAdvertisement() {
		return advertisementService.getOneDeclareGoodsAdvertisement();
	}

	/**
	 * 查询人脸识别广告
	 */
	@ResponseBody
	@RequestMapping("/queryFaceRecognitionAdvertisement")
	public Object queryFaceRecognitionAdvertisement(String deviceNumber, String data) {
		logger.info("AdvertisementController queryFaceRecognitionAdvertisement 输入data数据为：" + data + "; 机器码deviceNumber为： " + deviceNumber);
		if (StringUtils.isBlank(deviceNumber)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "终端编码不能为空");
		}
		if (StringUtils.isBlank(data)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
		}
		try {
			JSONObject dataJsonObj = JSON.parseObject(data);
			JSONArray ageSexJsonArray = dataJsonObj.getJSONArray(JSONConstant.JOSN_KEY_AGE_SEX_LIST);
			if (ageSexJsonArray == null || ageSexJsonArray.size() == 0) {
				return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
			}
			return advertisementService.getFaceRecognitionAdvertisementByAgeAndSex(deviceNumber, ageSexJsonArray);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController queryFaceRecognitionAdvertisement 失败原因为：" + e.getMessage());
			return new ControllerException(e.getMessage());
		}
	}


	/**
	 * 接收监播报告数据
	 */
	@ResponseBody
	@RequestMapping("/receiveMonitorData")
	public Object receiveMonitorData(String data) {
		logger.info("AdvertisementController receiveMonitorData 输入数据data为： " + data);
		//验证数据
		if (StringUtils.isBlank(data)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
		}
		JSONObject dataJSONObj = JSON.parseObject(data);
		JSONArray monitorDataJsonArray = dataJSONObj.getJSONArray(JSONConstant.JOSN_KEY_MONITOR_DATA_LIST);
		//验证数据不能为空
		if (monitorDataJsonArray == null || monitorDataJsonArray.size() == 0) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
		}
		try {
			//保存数据
			advertisementMonitorService.saveBatchAdvertisementMonitor(monitorDataJsonArray);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController receiveMonitorData 失败原因为：" + e.getMessage());
			return new ControllerException(e.getMessage());
		}
		return "保存数据成功";
	}

	/**
	 * 查询所有广告（不包含品宣广告）
	 */
	@ResponseBody
	@RequestMapping("/queryAllAdvertisements")
	public Object queryAllAdvertisements() {
		return advertisementService.listAdvertisement();
	}

	/**
	 * 报告已下载广告数量
	 */
	@ResponseBody
	@RequestMapping("/reportDownloadedAdvertisementNum")
	public Object reportDownloadedAdvertisementNum(String data) {
		//验证数据
		if (StringUtils.isBlank(data)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
		}
		DeviceAdvertisement deviceAdvertisement = JSON.parseObject(data, DeviceAdvertisement.class);
		if (deviceAdvertisement == null ) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "输入数据为空");
		}
		//机器码
		String deviceNumber = deviceAdvertisement.getDeviceNumber();
		if (StringUtils.isBlank(deviceNumber)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "机器码不能为空");
		}
		try {
			deviceAdvertisementService.reportDownloadedAdvertisementNum(deviceAdvertisement);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertisementController reportDownloadedAdvertisementNum 报告已下载广告数量失败：" + e.getMessage());
			return new ControllerException(e.getMessage());
		}
		return "";
	}

}
